﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SHKF.B2C.BaseHosted;
using SHKF.B2C.Hosted;
using SHKF.B2C.YouZanYun.BLL;
using SHKF.B2C.YouZanYun.Model.Dto.Token;
using SHKF.B2C.YouZanYun.Model.Entity;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
 

namespace SHKF.B2C.YouZanYun.Service.Controllers
{
    /// <summary>
    /// 有赞云
    /// </summary>
    [ApiController]
    [Route("[controller]")]
    public class TokenController : BaseController
    {
        private readonly TokenBLL tokenBLL;
       　
        public TokenController(TokenBLL tokenBLL,
            ILogger<TokenController> logger) : base(logger)
        {
            this.tokenBLL = tokenBLL;　
        }

        /// <summary>
        /// 获取Token
        /// </summary>
        /// <param name="app_poi_code">商店ID</param>
        /// <returns></returns>
        [HttpGet]
        public TokenDataDto Get(string authority_id)
        {
            TokenEntity requestToken = this.tokenBLL.Get(authority_id);
            TokenDataDto token = new TokenDataDto();
            if (requestToken != null)
            {
                token.expires = requestToken.expires;
                token.re_expires = requestToken.re_expires;
                token.scope = requestToken.scope;
                token.access_token = requestToken.access_token;
                token.refresh_token = requestToken.refresh_token;
                token.authority_id = requestToken.authority_id;
            }
            return token;
        }

        [HttpPost("Save")]
        public TokenDataDto Save([FromForm] TokenEntity info)
        {
            info.scope = "";
            this.tokenBLL.Save(info);
            TokenDataDto dto = this.Get(info.authority_id);
            return dto;
        }

        [HttpGet("GetToken")]
        public async Task<TokenYZYDto> GetToken(string authority_id)
        { 
            HttpClientYouZanYunS httpClient = new HttpClientYouZanYunS(null, this.tokenBLL);
            httpClient.authority_id = authority_id;
            TokenYZYDto dto = await httpClient.GetToken();
            return dto;
        }
    }
}
